using System;
using System.Collections.Generic;
using System.Text;
using Widgetsphere.GeneratorEngine;

namespace Omniture.API.Generators.Objects
{
	internal class PageCacheItemTemplate : BaseTemplate
	{
		internal PageCacheItemTemplate(OmnitureProject project)
			: base(project)
		{
		}

		public override string FileContent
		{
			get
			{
				var sb = new StringBuilder();
				sb.AppendLine("using System;");
				sb.AppendLine("using System.Collections.Generic;");
				sb.AppendLine("using System.Text;");
				sb.AppendLine("using System.Collections.ObjectModel;");
				sb.AppendLine("using System.IO;");
				sb.AppendLine("using System.Xml;");
				sb.AppendLine("using System.Xml.XPath;");
				sb.AppendLine();
				sb.AppendLine("namespace Omniture.Plugin");
				sb.AppendLine("{");
				sb.AppendLine("	[Serializable()]");
				sb.AppendLine("	public class PageCacheItem");
				sb.AppendLine("	{");
				sb.AppendLine("		#region Class Members");
				sb.AppendLine();
				sb.AppendLine("		private string _scriptCodeFile = string.Empty;");
				sb.AppendLine("		private System.Web.UI.Page _page = null;");
				sb.AppendLine("		protected EvarCache _evars = null;");
				sb.AppendLine("		protected PropCache _props = null;");
				sb.AppendLine("		protected EventCache _events = null;");
				sb.AppendLine("		private bool _trackDownloadLinks = " + this.Project.TrackDownloadLinks.ToString().ToLower() + ";");
				sb.AppendLine("		private bool _trackExternalLinks = " + this.Project.TrackExternalLinks.ToString().ToLower() + ";");
				sb.AppendLine("		private string _linkDownloadFileTypes = \"" + this.Project.LinkDownloadFileTypes + "\";");
				sb.AppendLine("		private string _linkInternalFilters = \"" + this.Project.LinkInternalFilters + "\";");
				sb.AppendLine("		private string _documentKey = string.Empty;");
				sb.AppendLine();
				sb.AppendLine("		#endregion");
				sb.AppendLine();
				sb.AppendLine("		public PageCacheItem(System.Web.UI.Page page)");
				sb.AppendLine("		{");
				sb.AppendLine("			_page = page;");
				sb.AppendLine("			_evars = new EvarCache(_page);");
				sb.AppendLine("			_props = new PropCache(_page);");
				sb.AppendLine("			_events = new EventCache(_page);");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		#region Property Implementations");
				sb.AppendLine();
				sb.AppendLine("		public System.Web.UI.Page Page");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _page; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string Products");
				sb.AppendLine("		{");
				sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".products\"]; }");
				sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".products\"] = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string FormName");
				sb.AppendLine("		{");
				sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".formName\"]; }");
				sb.AppendLine("			set");
				sb.AppendLine("			{");
				sb.AppendLine("				if (!string.IsNullOrEmpty(value) && !Omniture.API.TrackItemBase.ValidateName(value))");
				sb.AppendLine("					throw new Exception(\"The form name is not a valid token.\");");
				sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".formName\"] = value;");
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string PageName");
				sb.AppendLine("		{");
				sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".pageName\"]; }");
				sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".pageName\"] = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string Channel");
				sb.AppendLine("		{");
				sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".channel\"]; }");
				sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".channel\"] = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string ScriptCodeFile");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _scriptCodeFile; }");
				sb.AppendLine("		}");
				sb.AppendLine();				
				sb.AppendLine("		public EvarCache Evars");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _evars; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public PropCache Props");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _props; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public EventCache Events");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _events; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public bool TrackDownloadLinks");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _trackDownloadLinks; }");
				sb.AppendLine("			set { _trackDownloadLinks = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public bool TrackExternalLinks");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _trackExternalLinks; }");
				sb.AppendLine("			set { _trackExternalLinks = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string LinkDownloadFileTypes");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _linkDownloadFileTypes; }");
				sb.AppendLine("			set { _linkDownloadFileTypes = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string LinkInternalFilters");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _linkInternalFilters; }");
				sb.AppendLine("			set { _linkInternalFilters = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public string DocumentKey");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _documentKey; }");
				sb.AppendLine("			set { _documentKey = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		#endregion");
				sb.AppendLine();
				sb.AppendLine("		#region Methods");
				sb.AppendLine();
				sb.AppendLine("		internal bool Load(string urlPath)");
				sb.AppendLine("		{");
				sb.AppendLine("			string configFile = System.Configuration.ConfigurationManager.AppSettings[\"OmnitureSiteConfiguration\"];");
				sb.AppendLine("			if (!File.Exists(configFile))");
				sb.AppendLine("				configFile = System.Web.HttpContext.Current.Server.MapPath(configFile);");
				sb.AppendLine();
				sb.AppendLine("			if (!File.Exists(configFile))");
				sb.AppendLine("				throw new Exception(\"The Omniture configuration file '\" + configFile + \"' does not exist\");");
				sb.AppendLine();
				sb.AppendLine("			XPathDocument doc = new XPathDocument(configFile);");
				sb.AppendLine("			XPathNavigator configXmlNavigator = doc.CreateNavigator();");
				sb.AppendLine();
				sb.AppendLine("			//Setup Global values");
				sb.AppendLine("			XPathNodeIterator pageIterator = configXmlNavigator.Select(\"//omnitureconfig\");");
				sb.AppendLine("			pageIterator.MoveNext();");
				sb.AppendLine("			_scriptCodeFile = pageIterator.Current.GetAttribute(\"ScriptFilePath\", string.Empty);");
				sb.AppendLine("			_trackDownloadLinks = bool.Parse(pageIterator.Current.GetAttribute(\"TrackDownloadLinks\", string.Empty));");
				sb.AppendLine("			_trackExternalLinks = bool.Parse(pageIterator.Current.GetAttribute(\"TrackExternalLinks\", string.Empty));");
				sb.AppendLine("			_linkDownloadFileTypes = pageIterator.Current.GetAttribute(\"LinkDownloadFileTypes\", string.Empty);");
				sb.AppendLine("			_linkInternalFilters = pageIterator.Current.GetAttribute(\"LinkInternalFilters\", string.Empty);"); 
				sb.AppendLine("			_documentKey = pageIterator.Current.GetAttribute(\"DocumentKey\", string.Empty);"); 
				sb.AppendLine("			pageIterator = configXmlNavigator.Select(\"//omnitureconfig/pages/page[@url='\" + urlPath.ToLower() + \"']\");			");
				sb.AppendLine();
				sb.AppendLine("			string url = urlPath.ToLower();");
				sb.AppendLine("			pageIterator = configXmlNavigator.Select(\"//omnitureconfig/pages/page[@url='\" + url + \"']\");");
				sb.AppendLine("			while ((url.Length > 1) && (pageIterator.Count == 0))");
				sb.AppendLine("			{");
				sb.AppendLine("				string[] arr = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);");
				sb.AppendLine("				url = \"/\";");
				sb.AppendLine("				for (int ii = 0; ii < arr.Length - 1; ii++)");
				sb.AppendLine("					url += arr[0] + \"/\";");
				sb.AppendLine("				url += \"*\";");
				sb.AppendLine("				pageIterator = configXmlNavigator.Select(\"//omnitureconfig/pages/page[@url='\" + url + \"']\");");
				sb.AppendLine("				url = url.TrimEnd(new char[] { '*' });");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			this.PageName = string.Empty;");
				sb.AppendLine("			this.Channel = string.Empty;");
				sb.AppendLine("			this.FormName = string.Empty;");
				sb.AppendLine();

				sb.AppendLine("			//Clear Defaults");
				foreach (ITrackItem item in this.Project.EVarList)
					sb.AppendLine("			if (this.Evars.IsDefault" + item.CodeName + ") { this.Evars." + item.CodeName + " = string.Empty; this.Evars.IsDefault" + item.CodeName + " = false; }");
				foreach (ITrackItem item in this.Project.PropList)
					sb.AppendLine("			if (this.Props.IsDefault" + item.CodeName + ") { this.Props." + item.CodeName + " = string.Empty; this.Props.IsDefault" + item.CodeName + " = false; }");
				foreach (ITrackItem item in this.Project.EventList)
					sb.AppendLine("			if (this.Events.IsDefault" + item.CodeName + ") { this.Events." + item.CodeName + " = false; this.Events.IsDefault" + item.CodeName + " = false; }");

				sb.AppendLine();
				sb.AppendLine("			if (pageIterator.Count == 0)");
				sb.AppendLine("				return false;");				
				sb.AppendLine();
				sb.AppendLine("			if (pageIterator.Count > 1)");
				sb.AppendLine("				throw new ApplicationException(\"Invalid Omniture configuration. Multiple configurations were found for page: '\" + urlPath + \"'.\");");
				sb.AppendLine();
				sb.AppendLine("			pageIterator.MoveNext();");
				sb.AppendLine();
				sb.AppendLine("			this.PageName = pageIterator.Current.GetAttribute(\"pagename\", string.Empty);");
				sb.AppendLine("			this.Channel = pageIterator.Current.GetAttribute(\"channel\", string.Empty);");
				sb.AppendLine("			this.FormName = pageIterator.Current.GetAttribute(\"formname\", string.Empty);");
				sb.AppendLine();
				sb.AppendLine("			//Load static Evars");
				sb.AppendLine("			XPathNodeIterator evarList = pageIterator.Current.SelectChildren(\"evars\", string.Empty);");
				sb.AppendLine("			if (evarList.Current.MoveToChild(\"evars\", string.Empty))");
				sb.AppendLine("			{");
				sb.AppendLine("				if (evarList.Current.MoveToChild(\"evar\", string.Empty))");
				sb.AppendLine("				{");
				sb.AppendLine("					do");
				sb.AppendLine("					{						");
				sb.AppendLine("						string name = evarList.Current.GetAttribute(\"name\", string.Empty);");
				sb.AppendLine("						string defaultValue = evarList.Current.GetAttribute(\"default\", string.Empty);");
				sb.AppendLine("						this.Evars.SetValue(name, defaultValue);");
				sb.AppendLine("					} while (evarList.Current.MoveToNext());");
				sb.AppendLine("				}");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			//Load static Props");
				sb.AppendLine("			XPathNodeIterator propList = pageIterator.Current.SelectChildren(\"props\", string.Empty);");
				sb.AppendLine("			if (propList.Current.MoveToChild(\"props\", string.Empty))");
				sb.AppendLine("			{");
				sb.AppendLine("				if (propList.Current.MoveToChild(\"prop\", string.Empty))");
				sb.AppendLine("				{");
				sb.AppendLine("					do");
				sb.AppendLine("					{						");
				sb.AppendLine("						string name = propList.Current.GetAttribute(\"name\", string.Empty);");
				sb.AppendLine("						string defaultValue = propList.Current.GetAttribute(\"default\", string.Empty);");
				sb.AppendLine("						this.Props.SetValue(name, defaultValue);");
				sb.AppendLine("					} while (propList.Current.MoveToNext());");
				sb.AppendLine("				}");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			//Load static Events");
				sb.AppendLine("			XPathNodeIterator eventList = pageIterator.Current.SelectChildren(\"events\", string.Empty);");
				sb.AppendLine("			if (eventList.Current.MoveToChild(\"events\", string.Empty))");
				sb.AppendLine("			{");
				sb.AppendLine("				if (eventList.Current.MoveToChild(\"event\", string.Empty))");
				sb.AppendLine("				{");
				sb.AppendLine("					do");
				sb.AppendLine("					{						");
				sb.AppendLine("						string name = eventList.Current.GetAttribute(\"name\", string.Empty);");
				sb.AppendLine("						this.Events.SetValue(name, true);");
				sb.AppendLine("					} while (eventList.Current.MoveToNext());");
				sb.AppendLine("				}");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			return true;");
				sb.AppendLine();
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		#endregion");
				sb.AppendLine();
				sb.AppendLine("	}");				

				#region Add Prop Cache for the Page Configuration
				sb.AppendLine("	public class PropCache");
				sb.AppendLine("	{");

				#region Class Members

				sb.AppendLine("		private System.Web.UI.Page _page = null;");
				sb.AppendLine();

				#endregion

				sb.AppendLine("		internal PropCache(System.Web.UI.Page page)");
				sb.AppendLine("		{");
				sb.AppendLine("			_page = page;");
				sb.AppendLine("		}");

				#region Property Implementation

				foreach (ITrackItem item in this.Project.PropList)
				{
					sb.AppendLine("		/// <summary>");
					sb.AppendLine("		/// Maps to Omniture Prop: " + item.Name);
					if (!string.IsNullOrEmpty(item.Description))
						sb.AppendLine("		/// <para>" + item.Description + "</para>");
					sb.AppendLine("		/// </summary>");
					sb.AppendLine("		public string " + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + "\"]; }");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + "\"] = value; this.IsDefault" + item.CodeName + " = false; }");
					sb.AppendLine("		}");
					sb.AppendLine();

					//The default keeper property
					sb.AppendLine("		internal bool IsDefault" + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get");
					sb.AppendLine("			{");
					sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return false;");
					sb.AppendLine("				if (_page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + ".default\"] == null)");
					sb.AppendLine("					_page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + ".default\"] = false;");
					sb.AppendLine("				return (bool)_page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + ".default\"];");
					sb.AppendLine("			}");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".props." + item.Name + ".default\"] = value; }");
					sb.AppendLine("		}");
					sb.AppendLine();
				}

				#endregion

				#region Methods

				sb.AppendLine("		internal void SetValue(string name, string value)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch(name)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.PropList)
				{
					sb.AppendLine("				case \"" + item.Name + "\": if (this." + item.CodeName + " == string.Empty) this." + item.CodeName + " = value; this.IsDefault" + item.CodeName + " = true; break;");
				}
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();

				sb.AppendLine("		internal void Reset()");
				sb.AppendLine("		{");
				foreach (ITrackItem item in this.Project.PropList)
				{
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".props." + item.Name + "\");");
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".props." + item.Name + ".default\");");
				}
				sb.AppendLine("		}");
				sb.AppendLine();				

				#endregion

				sb.AppendLine("	}");
				sb.AppendLine();

				#endregion

				#region Add Evar Cache for the Page Configuration
				sb.AppendLine("		public class EvarCache");
				sb.AppendLine("		{");

				#region Class Members

				sb.AppendLine("		private System.Web.UI.Page _page = null;");
				sb.AppendLine();

				#endregion

				sb.AppendLine("		internal EvarCache(System.Web.UI.Page page)");
				sb.AppendLine("		{");
				sb.AppendLine("			_page = page;");
				sb.AppendLine("		}");
				
				#region Property Implementation

				foreach (ITrackItem item in this.Project.EVarList)
				{
					sb.AppendLine("		/// <summary>");
					sb.AppendLine("		/// Maps to Omniture EVar: " + item.Name);
					if (!string.IsNullOrEmpty(item.Description))
						sb.AppendLine("		/// <para>" + item.Description + "</para>");
					sb.AppendLine("		/// </summary>");
					sb.AppendLine("		public string " + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return string.Empty; return (string)_page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + "\"]; }");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + "\"] = value; this.IsDefault" + item.CodeName + " = false; }");
					sb.AppendLine("		}");
					sb.AppendLine();

					//The default keeper property
					sb.AppendLine("		internal bool IsDefault" + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get");
					sb.AppendLine("			{");
					sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return false;");
					sb.AppendLine("				if (_page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + ".default\"] == null)");
					sb.AppendLine("					_page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + ".default\"] = false;");
					sb.AppendLine("				return (bool)_page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + ".default\"];");
					sb.AppendLine("			}");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".evars." + item.Name + ".default\"] = value; }");
					sb.AppendLine("		}");
					sb.AppendLine();
				}

				#endregion

				#region Methods

				sb.AppendLine("		internal void SetValue(string name, string value)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch(name)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.EVarList)
				{
					sb.AppendLine("				case \"" + item.Name + "\": if (this." + item.CodeName + " == string.Empty) this." + item.CodeName + " = value; this.IsDefault" + item.CodeName + " = true; break;");
				}
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();

				sb.AppendLine("		internal void Reset()");
				sb.AppendLine("		{");
				foreach (ITrackItem item in this.Project.EVarList)
				{
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".evars." + item.Name + "\");");
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".evars." + item.Name + ".default\");");
				}
				sb.AppendLine("		}");
				sb.AppendLine();

				#endregion

				sb.AppendLine("	}");
				sb.AppendLine();

				#endregion

				#region Add Event Cache for the Page Configuration
				sb.AppendLine("	public class EventCache");
				sb.AppendLine("	{");

				#region Class Members

				sb.AppendLine("		private System.Web.UI.Page _page = null;");
				sb.AppendLine();
				sb.AppendLine("		public enum EventCacheConstants");
				sb.AppendLine("		{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("			" + item.CodeName + ",");
				}
				sb.AppendLine("		}");
				sb.AppendLine();

				#endregion

				sb.AppendLine("		internal EventCache(System.Web.UI.Page page)");
				sb.AppendLine("		{");
				sb.AppendLine("			_page = page;");
				sb.AppendLine("		}");

				#region Property Implementation

				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("		/// <summary>");
					sb.AppendLine("		/// Maps to Omniture Event: " + item.Name);
					if (!string.IsNullOrEmpty(item.Description))
						sb.AppendLine("		/// <para>" + item.Description + "</para>");
					sb.AppendLine("		/// </summary>");
					sb.AppendLine("		public bool " + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get { if (System.Web.HttpContext.Current.Session == null) return false; bool b; bool.TryParse((string)_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + "\"], out b); return b; }");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + "\"] = value.ToString(); this.IsDefault" + item.CodeName + " = false; }");
					sb.AppendLine("		}");
					sb.AppendLine();

					//The default keeper property
					sb.AppendLine("		internal bool IsDefault" + item.CodeName);
					sb.AppendLine("		{");
					sb.AppendLine("			get");
					sb.AppendLine("			{");
					sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return false;");
					sb.AppendLine("				if (_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".default\"] == null)");
					sb.AppendLine("					_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".default\"] = false;");
					sb.AppendLine("				return (bool)_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".default\"];");
					sb.AppendLine("			}");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".default\"] = value; }");
					sb.AppendLine("		}");
					sb.AppendLine();

					//The serializer property keeper
					sb.AppendLine("		internal bool " + item.CodeName + "SerializeInternal");
					sb.AppendLine("		{");
					sb.AppendLine("			get");
					sb.AppendLine("			{");
					sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return false;");
					sb.AppendLine("				if (_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".serializer\"] == null) return true;");
					sb.AppendLine("				else return (bool)_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".serializer\"];");
					sb.AppendLine("			}");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".serializer\"] = value; }");
					sb.AppendLine("		}");
					sb.AppendLine();

					//The serializer custom ID keeper
					//This is used to define a custom KEY for serialization, the default is a GUID
					sb.AppendLine("		internal string " + item.CodeName + "SerializeKEYInternal");
					sb.AppendLine("		{");
					sb.AppendLine("			get");
					sb.AppendLine("			{");
					sb.AppendLine("				if (System.Web.HttpContext.Current.Session == null) return null;");
					sb.AppendLine("				return (string)_page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".serializer.key\"];");
					sb.AppendLine("			}");
					sb.AppendLine("			set { if (System.Web.HttpContext.Current.Session == null) return; _page.Session[\"" + this.Project.DocumentKey + ".events." + item.Name + ".serializer.key\"] = value; }");
					sb.AppendLine("		}");
					sb.AppendLine();
				}

				#endregion

				#region Methods

				//The public serialization determinate, by default all events are serialized
				sb.AppendLine("		/// <summary>");
				sb.AppendLine("		/// Allows you to set the serialization setting of each event. By default all events are serialized.");
				sb.AppendLine("		/// </summary>");
				sb.AppendLine("		public void SetEventSerialization(EventCacheConstants eventItem, bool value)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch (eventItem)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("				case EventCacheConstants." + item.CodeName + ": this." + item.CodeName + "SerializeInternal = value; break;");
				}
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();

				//The public serialization key setter, default all are null and we use GUIDs
				sb.AppendLine("		/// <summary>");
				sb.AppendLine("		/// Allows you to define a custom serialization key for each event. By default all events are serialized with a GUID.");
				sb.AppendLine("		/// </summary>");
				sb.AppendLine("		public void SetEventSerializationKey(EventCacheConstants eventItem, string value)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch (eventItem)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("				case EventCacheConstants." + item.CodeName + ": this." + item.CodeName + "SerializeKEYInternal = value; break;");
				}
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();

				//Internal Setkey
				sb.AppendLine("		internal void SetValue(string name, bool value)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch(name)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("				case \"" + item.Name + "\": if (!this." + item.CodeName + ") this." + item.CodeName + " = value; this.IsDefault" + item.CodeName + " = true; break;");
				}
				sb.AppendLine("			}");
				sb.AppendLine("		}");
				sb.AppendLine();

				sb.AppendLine("		internal void Reset()");
				sb.AppendLine("		{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".events." + item.Name + "\");");
					sb.AppendLine("			_page.Session.Remove(\"" + this.Project.DocumentKey + ".events." + item.Name + ".default\");");
				}
				sb.AppendLine("		}");
				sb.AppendLine();

				#endregion

				sb.AppendLine("	}");
				sb.AppendLine();

				#endregion

				sb.AppendLine("}");
				sb.AppendLine();
				return sb.ToString();
			}
		}

		public static void Generate(OmnitureProject project, string destinationFolder)
		{
			PageCacheItemTemplate t = new PageCacheItemTemplate(project);
			t.Generate(destinationFolder);
		}

		public override string FileName
		{
			get { return @"PageCacheItem.cs"; }
		}

	}
}
